package com.mdoya.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.mdoya.utils.ThreadLocalContextUtils;
import com.sun.prism.impl.BaseContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

/**
 * @author mdoya
 * @version 1.0
 * @description:  MybatisPlus公共字段自动填充
 */
@Slf4j
@Component
public class MybatisPlusCommonFiledConfig implements MetaObjectHandler {
    @Autowired
    private HttpSession session; // 获取登录成功用户对象

    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("MybatisPlus公共字段添加员工: {}",metaObject);
        metaObject.setValue("createTime", LocalDateTime.now());
        metaObject.setValue("updateTime", LocalDateTime.now());
        metaObject.setValue("createUser", ThreadLocalContextUtils.getUserLongId());
        metaObject.setValue("updateUser", ThreadLocalContextUtils.getUserLongId());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("MybatisPlus公共字段修改员工: {}",metaObject);
        metaObject.setValue("updateTime", LocalDateTime.now());
        metaObject.setValue("updateUser", ThreadLocalContextUtils.getUserLongId());
    }
}
